from random import randint


initial_P=0
current_P=0
glob_round_number=0
average_food_earnings=0
'''history={round_number:(m,number_hunters/(P*(P-1)),average_food_earnings),recycle every 1000 sets'''
history={}
h_pool=0
s_pool=0

'''expected_m is value range which most player will choose 'h' '''
expected_m=0

def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
    if round_number==1:
        global initial_P
        initial_P=len(player_reputations)+1
    
    global current_P
    current_P=len(player_reputations)

    
    '''m_index is given by the m/(P-1), i.e. my contribution to get reward. If m_idex is 1, meaning I could set all my decisions to be 'h' so reward is
    guranted.'''
    m_index=m/(current_P)

    '''save round number to glob variable'''
    global glob_round_number
    glob_round_number=round_number

    global h_pool
    global s_pool
    h_pool=0
    s_pool=0
    hunt_decisions=[]
    list_=[]
    sorted_opponent={}

    
    #TEST WATER ROUND
    if round_number==1:
        
        '''80% h'''
        if m_index<1:
            
            random_list=[randint(1,5) for x in range(current_P)]
            for x in random_list:
                if x==1:hunt_decisions=hunt_decisions+['s']
                else:hunt_decisions=hunt_decisions+['h']
                
        '''60%'''
        if m_index>1 and m_index<(0.25*current_P):
            
            random_list=[randint(1,5) for x in range(current_P)]
            for x in random_list: 
                if x==1 or x==2:hunt_decisions=hunt_decisions+['s']
                else:hunt_decisions=hunt_decisions+['h']

        '''40%'''
        if m_index>=0.25*current_P and m_index<(0.5*current_P):
            
            random_list=[randint(1,5) for x in range(current_P)]
            for x in random_list:
                if x==1 or x==2:hunt_decisions=hunt_decisions+['h']
                else:hunt_decisions=hunt_decisions+['s']
                
        '''20%'''
        if m_index>=(0.5*current_P):

            random_list=[randint(1,5) for x in range(current_P)]
            for x in random_list:
                if x==1:hunt_decisions=hunt_decisions+['h']
                else:hunt_decisions=hunt_decisions+['s']

    #WHEN PLAYERS NUMBER IS SMALL, IGNORE M
    elif current_P<=50:
        x=0
        while x<current_P:
            if player_reputations[x]>=0.8:
                hunt_decisions=hunt_decisions+['s']
            elif player_reputations[x]>1.25*current_reputations:
                hunt_decisions=hunt_decisions+['s']
            elif player_reputations[x]<0.9*current_reputations:
                hunt_decisions=hunt_decisions+['s']
            else:
                hunt_decisions=hunt_decisions+['h']
                  

    #IF LAST ROUND MANY OPPONENT CHOSE 'S'
    elif history[round_number-1][1]<=0.3:
        h_pool=round(history[round_number-1][1]*current_P,0)
        s_pool=current_P-h_pool

        sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])

        while s_pool>0:
            list_=list_+['s']
            s_pool=s_pool-1
        while h_pool>0:
            list_=list_+['h']
            h_pool=h_pool-1
        assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
        x=0
        while x<current_P:
            hunt_decisions=hunt_decisions+[assigned_list[x][1]]
            x=x+1
               
    

    
    #PUMPING current_reputation while collecting data:
    elif round_number<=initial_P:
        
        if m_index<1:

            h_pool=round(0.8*current_P,0)
            s_pool=current_P-h_pool

            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1
                                             
            
        
        elif m_index>=1 and m_index<(0.25*current_P):
            
            h_pool=round(0.6*current_P,0)
            s_pool=current_P-h_pool
            
            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1

        
        elif m_index>=0.25*current_P and m_index<(0.5*current_P):

            h_pool=round(0.4*current_P,0)
            s_pool=current_P-h_pool
                        
            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
            print(current_P)      
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1
            print(current_P)
            print(list_)
            print(sorted_opponent)
            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1

        
        elif m_index>=(0.5*current_P):

            h_pool=round(0.2*current_P,0)
            s_pool=current_P-h_pool
                        
            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_oppontnet[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1

    #START BECOMING SLACKER
    elif round_number>initial_P and round_number<=2*initial_P:
        
        if m_index<1:

            h_pool=round(0.8*current_P,0)
            s_pool=current_P-h_pool

            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1


        elif m>=expected_m*current_P*(current_P+1):

            h_pool=round(0.5*current_P,0)
            s_pool=current_P-h_pool

            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1
        else:
            x=0
            while x<current_P:
                if player_reputations[x]>=0.8:
                    hunt_decisions=hunt_decisions+['s']
                elif player_reputations[x]>1.25*current_reputations:
                    hunt_decisions=hunt_decisions+['s']
                elif player_reputations[x]<0.9*current_reputations:
                    hunt_decisions=hunt_decisions+['s']
                else:
                    hunt_decisions=hunt_decisions+['h']

    #START BECOMING EVIL SLACKER
    elif round_number>=2*initial_P:
        
        if m_index<1:

            h_pool=round(0.4*current_P,0)
            s_pool=current_P-h_pool

            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1


        elif m>=expected_m*current_P*(current_P+1):

            h_pool=round(0.25*current_P,0)
            s_pool=current_P-h_pool

            sorted_opponent=sorted([(player_reputations[x],x) for x in range(current_P)],key=lambda k:k[0])
                        
            while s_pool>0:
                list_=list_+['s']
                s_pool=s_pool-1
            while h_pool>0:
                list_=list_+['h']
                h_pool=h_pool-1

            assigned_list=sorted([(sorted_opponent[x][1],list_[x]) for x in range(current_P)],key=lambda k:k[0])
            
            x=0
            while x<current_P:
                hunt_decisions=hunt_decisions+[assigned_list[x][1]]
                x=x+1

        else:
            x=0
            while x<current_P:
                if player_reputations[x]>=0.8:
                    hunt_decisions=hunt_decisions+['s']
                elif player_reputations[x]>1.25*current_reputations:
                    hunt_decisions=hunt_decisions+['s']
                elif player_reputations[x]<0.9*current_reputations:
                    hunt_decisions=hunt_decisions+['s']
                else:
                    hunt_decisions=hunt_decisions+['h']         

    return hunt_decisions

def hunt_outcomes(food_earnings):

    global average_food_earnings
    average_food_earnings=0
    average_food_earnings=float(sum(food_earnings))/len(food_earnings)


def round_end(award, m, number_hunters):
    #save history:
    global glob_round_number
    global current_P
    global history

    if len(history)>1000:
        sum_m=0
        sum_b=0
        sum_c=0
        for x in history.keys():
            sum_m=sum_m+history[x][0]
            sum_b=sum_b+history[x][1]
            sum_c=sum_c+history[x][2]
        history={glob_round_number:(sum_m/1000,sum_b/1000,sum_c/1000)}
    else:
        history=add_dict(history,{glob_round_number:(float(m),float(number_hunters)/(current_P*(current_P-1)),average_food_earnings)})

    if glob_round_number>1:
        opponents_analysis()

    
'''----------------------------------------------------------------SUBFUNCTIONS--------------------------------------------------------------------------'''

def add_dict(dict1,dict2):
    keys1=list(dict1.keys())
    keys2=list(dict2.keys())
    sum_keys=keys1+keys2

    values1=list(dict1.values())
    values2=list(dict2.values())
    sum_values=values1+values2

    return {sum_keys[x]:sum_values[x] for x in range(len(sum_keys))}
   
def opponents_analysis():
    
    global history

    '''to find what m value is which most player has higher chance to  choose 'h' '''
    global expected_m
    m_list=[]
    ''' pick m/(p)(p-1)) when probability of finding 'h' in each decision is higher '''
    for x in history.keys():
        if history[x][1]>0.5:
            m_list=m_list+[history[x][0]/(float(current_P+1)*(current_P))]

    if m_list!=0:
        expected_m=float(sum(m_list))/len(m_list)
    
    '''history={round_number:(m,number_hunters/(P*(P-1)),average_food_earnings),recycle every 1000 sets'''



























    
